import React, {Component} from "react";
import PropTyps from 'prop-types';
export default class Footer extends Component {
  static propTyps = {
    todos: PropTyps.array.isRequired,
    updateAllTodo: PropTyps.func.isRequired
  }

  // 全选/反选按钮事件
  handleCheck = (e) => {    
    this.props.updateAllTodo(e.target.checked);
  }

  // 清楚已完成数据 
  deleteAll = () => {
    if (window.confirm('确实是否全部删除?')) {
      this.props.deleteAllTodo();
    }
  }
  render () {
    const {todos} = this.props;
    const doneCount = todos.filter(v=>{
      return v.done;
    }).length;
    const total = todos.length;
    return (
      <div className="todo-footer">
        <label>
          <input type="checkbox" checked={total && doneCount === total} disabled={!total} onChange={this.handleCheck} />
        </label>
        <span>
          <span>已完成{doneCount}</span> / 全部{total}
        </span>
        <button onClick={this.deleteAll} className="btn btn-danger">清除已完成任务</button>
      </div>
    )
  }
}